﻿using System;
using System.Text.RegularExpressions;

namespace Rcon
{
	/// <summary>
	/// A match combines an IMap and an IGameType
	/// </summary>
	public class Match
	{
		private IMap map = null;
		private IGameType gameType = null;

        public Match() { }
        
        public Match(IMap map, IGameType gameType)
		{
			if (map == null || gameType == null)
			{
				throw new ArgumentNullException("Map and game type may not be null.");
			}

			this.map = map;
			this.gameType = gameType;
		}

		public IMap Map
		{
			get { return map; }
		}

		public IGameType GameType
		{
			get { return gameType; }
		}

		public override string ToString()
		{
            return gameType.GameName + " on " + map.PrettyName;
		}

        public string ToUglyString()
        {
            return gameType.PlayListNum.ToString() + " " + map.MapName;
        }

        public Match FromUglyString(string str)
        {
            Match match;
            string[] args = Regex.Split(str, " ");
            MapLookup map = new MapLookup();
            GametypeLookup game = new GametypeLookup();
            if (map.IsValidName(args[1]) && game.IsValidPlayList(args[0]))
            {
                int playlistNum = Convert.ToInt16(args[0]);
                match = new Match(new Map(args[1], map.GetPrettyName(args[1])), new GameType(playlistNum, game.GetPlayListName(playlistNum)));
            }
            else
                match = null;
            return match;
        }

        public Match FromPrettyString(string str)
        {
            Match match;
            string[] args = Regex.Split(str, " on ");
            MapLookup map = new MapLookup();
            GametypeLookup game = new GametypeLookup();
            if (map.IsValidName(args[1]) && game.IsValidPlayList(args[0]))
                match = new Match(new Map(map.GetUglyName(args[1]), args[1]), new GameType(game.GetPlayListNumber(args[0]), args[0]));
            else
                match = null;
            return match;
        }
	}
}
